home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / include / glibmm-2.4 / glibmm / object.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-20  |  9.1 KB  |  283 lines

  1. // -*- c++ -*-
  2. #ifndef _GLIBMM_OBJECT_H
  3. #define _GLIBMM_OBJECT_H
  4. /* $Id: object.h,v 1.10 2005/01/21 19:26:04 murrayc Exp $ */
  5.  
  6. /* Copyright 2002 The gtkmm Development Team
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23. #include <glibmm/objectbase.h>
  24. #include <glibmm/wrap.h>
  25. #include <glibmm/quark.h>
  26. #include <glibmm/refptr.h>
  27. #include <glibmm/utility.h> /* Could be private, but that would be tedious. */
  28. #include <glibmm/containerhandle_shared.h> //Because its specializations may be here.
  29. #include <glibmm/value.h>
  30.  
  31. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  32. extern "C"
  33. {
  34. typedef struct _GObject GObject;
  35. typedef struct _GObjectClass GObjectClass;
  36. }
  37. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  38.  
  39.  
  40. namespace Glib
  41. {
  42.  
  43. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  44.  
  45. class Class;
  46. class Object_Class;
  47. class GSigConnectionNode;
  48.  
  49. /* ConstructParams::ConstructParams() takes a varargs list of properties
  50.  * and values, like g_object_new() does.  This list will then be converted
  51.  * to a GParameter array, for use with g_object_newv().  No overhead is
  52.  * involved, since g_object_new() is just a wrapper around g_object_newv()
  53.  * as well.
  54.  *
  55.  * The advantage of an auxilary ConstructParams object over g_object_new()
  56.  * is that the actual construction is always done in the Glib::Object ctor.
  57.  * This allows for neat tricks like easy creation of derived custom types,
  58.  * without adding special support to each ctor of every class.
  59.  *
  60.  * The comments in object.cc and objectbase.cc should explain in detail
  61.  * how this works.
  62.  */
  63. class ConstructParams
  64. {
  65. public:
  66.   const Glib::Class&  glibmm_class;
  67.   unsigned int        n_parameters;
  68.   GParameter*         parameters;
  69.  
  70.   explicit ConstructParams(const Glib::Class& glibmm_class_);
  71.   ConstructParams(const Glib::Class& glibmm_class_, const char* first_property_name, ...);
  72.   ~ConstructParams();
  73.  
  74.   // This is only used by the C++ compiler (since g++ 3.4) to create temporary instances.
  75.   // Apparently the compiler will actually optimize away the use of this.
  76.   // See bug #132300.
  77.   ConstructParams(const ConstructParams& other);
  78.  
  79. private:
  80.   // noncopyable 
  81.   ConstructParams& operator=(const ConstructParams&);
  82. };
  83.  
  84. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  85.  
  86.  
  87. class Object : virtual public ObjectBase
  88. {
  89. public:
  90. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  91.   typedef Object       CppObjectType;
  92.   typedef Object_Class CppClassType;
  93.   typedef GObject      BaseObjectType;
  94.   typedef GObjectClass BaseClassType;
  95. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  96.  
  97. protected:
  98.   Object(); //For use by C++-only sub-types.
  99.   explicit Object(const Glib::ConstructParams& construct_params);
  100.   explicit Object(GObject* castitem);
  101.   virtual ~Object(); //It should only be deleted by the callback.
  102.  
  103. public:
  104.   //static RefPtr<Object> create(); //You must reimplement this in each derived class.
  105.  
  106. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  107.   static GType get_type()      G_GNUC_CONST;
  108.   static GType get_base_type() G_GNUC_CONST;
  109. #endif
  110.  
  111.   //GObject* gobj_copy(); //Give a ref-ed copy to someone. Use for direct struct access.
  112.  
  113.   // Glib::Objects contain a list<Quark, pair<void*, DestroyNotify> >
  114.   // to store run time data added to the object at run time.
  115.   //TODO: Use slots instead:
  116.   void* get_data(const QueryQuark &key);
  117.   void set_data(const Quark &key, void* data);
  118.   typedef void (*DestroyNotify) (gpointer data);
  119.   void set_data(const Quark &key, void* data, DestroyNotify notify);
  120.   void remove_data(const QueryQuark& quark);
  121.   // same as remove without notifying
  122.   void* steal_data(const QueryQuark& quark);
  123.  
  124.   // convenience functions
  125.   //template <class T>
  126.   //void set_data_typed(const Quark& quark, const T& data)
  127.   //  { set_data(quark, new T(data), delete_typed<T>); }
  128.  
  129.   //template <class T>
  130.   //T& get_data_typed(const QueryQuark& quark)
  131.   //  { return *static_cast<T*>(get_data(quark)); }
  132.  
  133. #ifndef DOXYGEN_SHOULD_SKIP_THIS
  134.  
  135. private:
  136.   friend class Glib::Object_Class;
  137.   static CppClassType object_class_;
  138.  
  139.   // noncopyable
  140.   Object(const Object&);
  141.   Object& operator=(const Object&);
  142.  
  143. #endif /* DOXYGEN_SHOULD_SKIP_THIS */
  144.  
  145.   // Glib::Object can not be dynamic because it lacks a float state.
  146.   //virtual void set_manage();
  147. };
  148.  
  149.  
  150. //For some (proably, more spec-compliant) compilers, these specializations must
  151. //be next to the objects that they use.
  152. #ifndef GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
  153. #ifndef DOXYGEN_SHOULD_SKIP_THIS /* hide the specializations */
  154.  
  155. namespace Container_Helpers
  156. {
  157.  
  158. /** Partial specialization for pointers to GObject instances.
  159.  * @ingroup ContHelpers
  160.  * The C++ type is always a Glib::RefPtr<>.
  161.  */
  162. template <class T>
  163. struct TypeTraits< Glib::RefPtr<T> >
  164. {
  165.   typedef Glib::RefPtr<T>              CppType;
  166.   typedef typename T::BaseObjectType * CType;
  167.   typedef typename T::BaseObjectType * CTypeNonConst;
  168.  
  169.   static CType   to_c_type      (const CppType& ptr) { return Glib::unwrap(ptr);     }
  170.   static CType   to_c_type      (CType          ptr) { return ptr;                   }
  171.   static CppType to_cpp_type    (CType          ptr)
  172.   {
  173.     //return Glib::wrap(ptr, true);
  174.  
  175.     //We copy/paste the wrap() implementation here,
  176.     //because we can not use a specific Glib::wrap(CType) overload here,
  177.     //because that would be "dependent", and g++ 3.4 does not allow that.
  178.     //The specific Glib::wrap() overloads don't do anything special anyway.
  179.     GObject* cobj = (GObject*)const_cast<CTypeNonConst>(ptr);
  180.     return Glib::RefPtr<T>( dynamic_cast<T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
  181.     //We use dynamic_cast<> in case of multiple inheritance.
  182.   }
  183.   
  184.   static void    release_c_type (CType          ptr)
  185.   {
  186.     GLIBMM_DEBUG_UNREFERENCE(0, ptr);
  187.     g_object_unref(ptr);
  188.   }
  189. };
  190.  
  191. //This confuse the SUN Forte compiler, so we ifdef it out:
  192. #ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  193.  
  194. /** Partial specialization for pointers to const GObject instances.
  195.  * @ingroup ContHelpers
  196.  * The C++ type is always a Glib::RefPtr<>.
  197.  */
  198. template <class T>
  199. struct TypeTraits< Glib::RefPtr<const T> >
  200. {
  201.   typedef Glib::RefPtr<const T>              CppType;
  202.   typedef const typename T::BaseObjectType * CType;
  203.   typedef typename T::BaseObjectType *       CTypeNonConst;
  204.  
  205.   static CType   to_c_type      (const CppType& ptr) { return Glib::unwrap(ptr);     }
  206.   static CType   to_c_type      (CType          ptr) { return ptr;                   }
  207.   static CppType to_cpp_type    (CType          ptr)
  208.   {
  209.     //return Glib::wrap(ptr, true);
  210.  
  211.     //We copy/paste the wrap() implementation here,
  212.     //because we can not use a specific Glib::wrap(CType) overload here,
  213.     //because that would be "dependent", and g++ 3.4 does not allow that.
  214.     //The specific Glib::wrap() overloads don't do anything special anyway.
  215.     GObject* cobj = (GObject*)(ptr);
  216.     return Glib::RefPtr<const T>( dynamic_cast<const T*>(Glib::wrap_auto(cobj, true /* take_copy */)) );
  217.     //We use dynamic_cast<> in case of multiple inheritance.
  218.   }
  219.   
  220.   static void    release_c_type (CType          ptr)
  221.   {
  222.     GLIBMM_DEBUG_UNREFERENCE(0, ptr);
  223.     g_object_unref(const_cast<CTypeNonConst>(ptr));
  224.   }
  225. };
  226.  
  227. #endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  228.  
  229. } //namespace Container_Helpers
  230.  
  231.  
  232. template <class T, class PtrT> inline
  233. PtrT Value_Pointer<T,PtrT>::get_(Glib::Object*) const
  234. {
  235.   return dynamic_cast<T*>(get_object());
  236. }
  237.  
  238.  
  239. /** Partial specialization for RefPtr<> to Glib::Object.
  240.  * @ingroup glibmmValue
  241.  */
  242. template <class T>
  243. class Value< Glib::RefPtr<T> > : public ValueBase_Object
  244. {
  245. public:
  246.   typedef Glib::RefPtr<T>             CppType;
  247.   typedef typename T::BaseObjectType* CType;
  248.  
  249.   static GType value_type() { return T::get_base_type(); }
  250.  
  251.   void set(const CppType& data) { set_object(data.operator->()); }
  252.   CppType get() const           { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
  253. };
  254.  
  255. //The SUN Forte Compiler has a problem with this: 
  256. #ifdef GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  257.  
  258. /** Partial specialization for RefPtr<> to const Glib::Object.
  259.  * @ingroup glibmmValue
  260.  */
  261. template <class T>
  262. class Value< Glib::RefPtr<const T> > : public ValueBase_Object
  263. {
  264. public:
  265.   typedef Glib::RefPtr<const T>       CppType;
  266.   typedef typename T::BaseObjectType* CType;
  267.  
  268.   static GType value_type() { return T::get_base_type(); }
  269.  
  270.   void set(const CppType& data) { set_object(const_cast<T*>(data.operator->())); }
  271.   CppType get() const           { return Glib::RefPtr<T>::cast_dynamic(get_object_copy()); }
  272. };
  273. #endif //GLIBMM_HAVE_DISAMBIGUOUS_CONST_TEMPLATE_SPECIALIZATIONS
  274.  
  275.  
  276. #endif //DOXYGEN_SHOULD_SKIP_THIS
  277. #endif //GLIBMM_CAN_USE_DYNAMIC_CAST_IN_UNUSED_TEMPLATE_WITHOUT_DEFINITION
  278.  
  279. } // namespace Glib
  280.  
  281. #endif /* _GLIBMM_OBJECT_H */
  282.  
  283.